package com.lly.songhigh.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lly.songhigh.R;
import com.lly.songhigh.entity.SearchBlur;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 李海龙 on 15/10/27.
 */
public class SearchBlurListViewAdapter extends BaseAdapter {

    private Context context;
    private List<SearchBlur.DataEntity.SongEntity> songEntities;

    public SearchBlurListViewAdapter(Context context, List<SearchBlur.DataEntity.SongEntity> songEntities) {
        this.context = context;
        this.songEntities = songEntities;
    }

    @Override
    public int getCount() {
        return songEntities != null && songEntities.size() > 0 ? songEntities.size() : 0;
    }

    @Override
    public SearchBlur.DataEntity.SongEntity getItem(int position) {
        return songEntities.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.search_blur_listview_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        SearchBlur.DataEntity.SongEntity songEntity = getItem(position);
        holder.searchBlurSongName.setText(songEntity.getName());
        holder.searchBlurSingerName.setText(songEntity.getSinger_name());

        return convertView;
    }

    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'search_blur_listview_item.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers (http://github.com/avast)
     */
    static class ViewHolder {
        @Bind(R.id.search_blur_head)
        ImageView searchBlurHead;
        @Bind(R.id.search_blur_songName)
        TextView searchBlurSongName;
        @Bind(R.id.search_blur_singerName)
        TextView searchBlurSingerName;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
